package com.neusoft.courseSelect.controller;

import com.neusoft.courseSelect.service.IAIService;
import com.neusoft.data.domain.SerCourse;
import com.neusoft.data.domain.SerKeyQuestion;
import com.neusoft.data.domain.SerKeyWord;
import com.neusoft.data.domain.SerServiceEvaluation;
import com.neusoft.data.service.ISerCourseService;
import com.neusoft.data.service.ISerKeyQuestionService;
import com.neusoft.data.service.ISerKeyWordService;
import com.neusoft.data.service.ISerServiceEvaluationService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.sun.jna.platform.unix.solaris.LibKstat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Api("选课页面")
@RestController
@RequestMapping("/select")
public class courseSelectionController extends BaseController {

    @Autowired
    private ISerKeyQuestionService keyQuestionService;

    @Autowired
    private ISerCourseService courseService;

    @Autowired
    private ISerKeyWordService keyWordService;

    @Autowired
    private IAIService aiService;

    @Autowired
    private ISerServiceEvaluationService evaluationService;

    @Autowired
    private RedisCache redisCache;

    @ApiOperation("获取课程信息")
    @GetMapping("/course")
    public TableDataInfo getCourse(String courseType) {
        System.out.println(SecurityUtils.getLoginUser().getUser());
        startPage();
        SerCourse course = new SerCourse();
        course.setStatus("1");
        course.setCourseType(courseType);
        List<SerCourse> list = courseService.selectSerCourseList(course);
        return getDataTable(list);
    }

    @ApiOperation("获取关键问题")
    @GetMapping("/keyQuestion")
    public TableDataInfo getKeyQuestion() {
        startPage();
        SerKeyQuestion keyQuestion = new SerKeyQuestion();
        keyQuestion.setStatus("1");
        List<SerKeyQuestion> list = keyQuestionService.selectSerKeyQuestionList(keyQuestion);
        return getDataTable(list);
    }

    @ApiOperation("获取关键词")
    @GetMapping("/keyWord")
    public TableDataInfo getKeyWord() {
        startPage();
        SerKeyWord keyWord = new SerKeyWord();
        keyWord.setStatus("1");
        List<SerKeyWord> list = keyWordService.selectSerKeyWordList(keyWord);
        return getDataTable(list);
    }

    @ApiOperation("根据关键词获取关键课程")
    @PostMapping("/keyCourse")
    public AjaxResult getKeyCourse(@RequestBody SerKeyWord keyWord) {
        SerCourse course = new SerCourse();
        course.setCourseKeyWord(keyWord.getKeyWordName());
        course.setStatus("1");
        List<SerCourse> list = courseService.selectSerCourseList(course);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据问题，进行回复")
    @GetMapping("/answer")
    public AjaxResult answer(@RequestParam String question, String realName) {
        String username = getLoginUser().getUsername();
        if (username.equals("admin")) {
            username = "1";
        }

        String verifyKey = CacheConstants.CHAT_SESSION + username;
        String sessionId = redisCache.getCacheObject(verifyKey);
        if (sessionId == null) {
            sessionId = UUID.randomUUID().toString();
        } else {
            redisCache.deleteObject(verifyKey);
        }
        redisCache.setCacheObject(verifyKey, sessionId, 10, TimeUnit.MINUTES);


        String result = aiService.answerQuestion(question, Long.valueOf(username), sessionId, realName);

        if (StringUtils.isNull(result)) {
            return AjaxResult.error("服务器错误");
        } else {
            return AjaxResult.success("调用成功",result);
        }
    }

    @ApiOperation("添加服务评价")
    @PostMapping("/evaluation")
    public AjaxResult addEvaluation(@RequestBody SerServiceEvaluation evaluation) {
        evaluationService.insertSerServiceEvaluation(evaluation);
        return AjaxResult.success("添加成功");
    }
}
